package cn.kattomy.shop.order.adminaction;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.kattomy.shop.order.service.OrderService;
import cn.kattomy.shop.order.vo.Order;
import cn.kattomy.shop.order.vo.OrderItem;
import cn.kattomy.shop.utils.PageBean;

public class AdminOrderAction extends ActionSupport implements ModelDriven<Order> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//模型驱动使用的对象
	private Order order = new Order();
	@Override
	public Order getModel() {
		return order;
	}
	
	//注入 OrderService
	private OrderService orderService;
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	
	/**
	 * 接收 pageNo 
	 */
	public Integer pageNo;
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
	
	//带分页查询的执行方法：
	public String findAll(){
		System.out.println(pageNo);
		//分页查询
		PageBean<Order> pageBean = orderService.findByPageNo(pageNo);
		//通过值栈保存数据到页面
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		//页面跳转
		return "findAll";
	}
	
	//根据订单 id 查询订单项
	public String findOrderItem(){
		System.out.println(order.getOid());
//		List<OrderItem> list = orderService.findOrderItem(order.getOid());
		Order orders = orderService.findByOid(order.getOid());
		//通过值栈显示到页面上
//		ActionContext.getContext().getValueStack().set("list", list);
		ActionContext.getContext().getValueStack().set("orders", orders);
		return "findOrderItem";
	}
	
	public String updateState(){
		//1.根据订单 id 查询订单
		Order currOrder = orderService.findByOid(order.getOid());
		System.out.println(currOrder);
		//2.修改订单状态
		currOrder.setState(3);
		orderService.update(currOrder);
		//页面跳转
		
		return "updateState";
	}
	
	public String delete(){
		System.out.println(">>>>>>>>>>>" + order.getOid());
		order = orderService.findByOid(order.getOid());
		orderService.delete(order);
		ActionContext.getContext().getValueStack().set("pageNo", pageNo);
		return "delete";
	}
}
